// pages/pick_good/pick_good.js
var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose_nav: 0,
        date: "2018-10-18",
        data: [],
        page_sign: "my",
        clerk: [],
        isIPX: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX,
            domain: app.globalData.domain.slice(0, -10)
        });
        var that = this;
        that.setData({
            clerk: wx.getStorageSync("clerk")
        });
        this.getData();
        this.getLeader();
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        let currentdate = year + seperator1 + month + seperator1 + strDate;
        this.setData({
            date: currentdate
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    choose_nav: function(e) {
        var id = e.currentTarget.dataset.id;
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/waitpick/mymemeber_waitpick2",
            method: "post",
            data: {
                openid: openid,
                day: id
            },
            success: res => {
                that.setData({
                    data: res.extra
                });
            }
        });
        this.setData({
            choose_nav: id
        });
    },
    bindDateChange: function(e) {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/waitpick/mymemeber_waitpick2",
            method: "post",
            data: {
                openid: openid,
                check_one_day: e.detail.value
            },
            success: res => {
                that.setData({
                    data: res.extra
                });
            }
        });
        this.setData({
            date: e.detail.value
        });
    },
    getData: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/waitpick/mymemeber_waitpick2",
            method: "post",
            data: {
                openid: openid,
                day: 0
            },
            success: res => {
                that.setData({
                    data: res.extra
                });
            }
        });
    },
    scancode: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        wx.scanCode({
            success: res => {
                wx.navigateTo({
                    url: "/" + res.path,
                    success() {},
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: "请扫描正确的提货二维码"
                        });
                    }
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: "请扫描正确的提货二维码",
                    showCancel: false
                });
            }
        });
    },
    alert: function() {
        var alert = this.data.alert;
        if (alert == 0) {
            alert = 1;
        } else {
            alert = 0;
        }
        this.setData({
            alert: alert
        });
    },
    alert2: function() {
        var alert2 = this.data.alert2;
        if (alert2 == 0) {
            alert2 = 1;
        } else {
            alert2 = 0;
        }
        this.setData({
            alert2: alert2
        });
    },
    getLeader: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        // 基本信息获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_personal",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
            }
        });
        // 旗下会员获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_children",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    member_num: res.extra
                });
            }
        });
        //旗下团长获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_recommend",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    group_num: res.extra
                });
            }
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    }
});